package com.cloud.drore.eboss.supplier.service.impl;

import com.cloud.drore.eboos.common.base.BaseServiceImpl;
import com.cloud.drore.eboos.common.base.MyMapper;
import com.cloud.drore.eboos.common.base.PagePo;
import com.cloud.drore.eboos.common.base.ResultMessage;
import com.cloud.drore.eboos.common.entity.supplier.ProductRelation;
import com.cloud.drore.eboos.common.entity.supplier.Ticket;
import com.cloud.drore.eboos.common.entity.supplier.vo.ConsumeAndPoiForDealer;
import com.cloud.drore.eboos.common.entity.supplier.vo.TicketAllVo;
import com.cloud.drore.eboos.exception.BusinessException;
import com.cloud.drore.eboss.operation.supplierApiService.IConsumeSupplierApiService;
import com.cloud.drore.eboss.supplier.entity.po.ProductRelationPo;
import com.cloud.drore.eboss.supplier.entity.vo.ProductQueryVo;
import com.cloud.drore.eboss.supplier.mapper.CombinationProductRelationMapper;
import com.cloud.drore.eboss.supplier.mapper.ProductConsumeRelationMapper;
import com.cloud.drore.eboss.supplier.mapper.ProductRelationMapper;
import com.cloud.drore.eboss.supplier.mapper.TicketMapper;
import com.cloud.drore.eboss.supplier.operationApiService.IProductRelationOperationApiService;
import com.cloud.drore.eboss.supplier.service.ICombinationProductService;
import com.cloud.drore.eboss.supplier.service.IProductRelationService;
import com.cloud.drore.eboss.supplier.service.ITicketService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 浙江卓锐科技股份有限公司 版权所有  Copyright 2018<br>
 *
 * @Description: <br>
 * @Project: eboos <br>
 * @CreateDate: Created in 2018/1/9 11:01 <br>
 * @Author: <a href="575842778@qq.com">郑永涛</a>
 */
@Service
public class ProductRelationServiceImpl extends BaseServiceImpl implements IProductRelationService,IProductRelationOperationApiService {

    @Autowired
    ProductRelationMapper productRelationMapper;
    @Resource
    IConsumeSupplierApiService consumeSupplierApiService;
    @Autowired
    ProductConsumeRelationMapper consumeRelationMapper;
    @Autowired
    TicketMapper ticketMapper;
    @Autowired
    CombinationProductRelationMapper combinationProductRelationMapper;
    @Resource
    ITicketService ticketService;
    @Resource
    ICombinationProductService combinationProductService;

    static final String COMBINATION_PRODUCT = "COMBINATION_PRODUCT";
    static final String TICKET_PRODUCT  = "TICKET_PRODUCT";

    @Override
    public MyMapper getMapper() {
        return productRelationMapper;
    }

    public ResultMessage findByName(PagePo page) {
        PageHelper.startPage(page);
        List list = productRelationMapper.selectAll();
        return new ResultMessage(new PageInfo<>(list));
    }

    public List<ProductRelation> findByParam(ProductRelationPo productRelation) {
        List productRelationList = productRelationMapper.findByParam(productRelation);
        return productRelationList;
    }

    public Long selectIdByTypeAndId(String type,Long productId){
        Long id = productRelationMapper.selectIdByTypeAndId(type,productId);
        return id;
    }

    public ProductRelation findById(Long id){
        ProductRelation relation = productRelationMapper.selectByPrimaryKey(id);
        ArrayList consumeList = new ArrayList<>();
        List<Long> consumeIds= consumeRelationMapper.selectConsumeIdByProductId(relation.getId());
        if(consumeIds!=null && consumeIds.size()!=0){
            for(Long consumeId:consumeIds){
                Object consume =  consumeSupplierApiService.selectByPrimaryKey(id);
                consumeList.add(consume);
            }
        }
        relation.setConsumeList(consumeList);
        return relation;
    }

    /**
     * @ClassName: ProductRelationServiceImpl
     * @param id
     * @Return: java.util.List<java.lang.Long>
     * @Decription: 根据供应商id查除组合产品以外id
     * @CreateDate: Created in 2018/1/25 18:35
     * @Author: <a href="575842778@qq.com">郑永涛</a>
     * @Modify:
     */
    public List<ProductQueryVo> selectProductIdBySupplierId(Long id){
        List<ProductQueryVo> productIds = productRelationMapper.selectProductIdBySupplierId(id);
        return productIds;
    }

    /**
     * @ClassName: ProductRelationServiceImpl
     * @param proNum
     * @Return: com.cloud.drore.eboos.common.entity.supplier.ProductRelation
     * @Decription: 根据产品编号查公共产品对象
     * @CreateDate: Created in 2018/1/31 14:43
     * @Author: <a href="575842778@qq.com">郑永涛</a>
     * @Modify:
     */
    public ProductRelation selectProductByProNum(String proNum)throws BusinessException{
        ProductRelation productRelation = productRelationMapper.selectProductByProNum(proNum);
        ArrayList list = new ArrayList<>();
        List<Long> consumeIds= consumeRelationMapper.selectConsumeIdByProductId(productRelation.getId());
        if(consumeIds!=null && consumeIds.size()!=0){
            for(Long id:consumeIds){
                Object consume =  consumeSupplierApiService.selectByPrimaryKey(id);
                list.add(consume);
            }
            productRelation.setConsumeList(list);
        }
        return productRelation;
    }

    /**
     * @param
     * @ClassName: FinaceOperationServiceImpl
     * @Return: com.cloud.drore.eboos.common.base.ResultMessage<com.cloud.drore.eboss.operation.entity.FinaceSourseDataOperation>
     * @Decription: 通过所有查询产品编号
     * @CreateDate: Created in 2018/1/29 0029 下午 4:59
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public ResultMessage<List<ProductQueryVo>> queryProductNumDealer() {
        List<ProductQueryVo> productIdList = productRelationMapper.selectProductNum();
        return new ResultMessage(productIdList);
    }

    /**
     * @ClassName: ProductRelationServiceImpl
     * @param proNum
     * @Return: java.util.List<com.cloud.drore.eboos.common.entity.supplier.vo.TicketAllVo>
     * @Decription: 根据产品编号查询子产品集合
     * @CreateDate: Created in 2018/3/6 16:55
     * @Author: <a href="575842778@qq.com">郑永涛</a>
     * @Modify:
     */
    public List<ProductRelation> selectProductListByProNum(String proNum)throws BusinessException{
        List<ProductRelation> productRelations = new ArrayList<ProductRelation>();
        ProductRelation productRelation = productRelationMapper.selectProductByProNum(proNum);
        if(productRelation == null){
            throw new BusinessException("未查到数据!");
        }
        List<TicketAllVo> list = new ArrayList<>();
        //判断是门票或者组合产品
        if(TICKET_PRODUCT.equals(productRelation.getProductType())){
            productRelations.add(productRelation);

        }else if (COMBINATION_PRODUCT.equals(productRelation.getProductType())){
            List<Long> productId = combinationProductRelationMapper.selectProductIdByCombinationProductId(productRelation.getProductId());
            for (Long ticketId : productId){
                Ticket ticket = ticketMapper.selectByPrimaryKey(ticketId);
                ProductRelation relation = productRelationMapper.selectByPrimaryKey(ticket.getCommonProductId());
                productRelations.add(relation);
            }
        }
        return productRelations;
    }


    /**
     * @ClassName: ProductRelationServiceImpl
     * @param type
     * @param productId
     * @Return: com.cloud.drore.eboos.common.entity.supplier.ProductRelation
     * @Decription:
     * @CreateDate: Created in 2018/3/6 21:11
     * @Author: <a href="575842778@qq.com">郑永涛</a>
     * @Modify:
     */
    public ProductRelation selectProductByTypeAndId(String type,Long productId){
        ProductRelation productRelation = productRelationMapper.selectProductByTypeAndId(type, productId);
        return productRelation;
    }

    /**
     * @ClassName: ProductRelationServiceImpl
     * @param relation
     * @Return: int
     * @Decription:
     * @CreateDate: Created in 2018/3/7 20:01
     * @Author: <a href="575842778@qq.com">郑永涛</a>
     * @Modify:
     */
    public int updateProductByParam(ProductRelation relation){
        return productRelationMapper.updateProductByParam(relation);
    }

    /**
     * @ClassName: ProductRelationServiceImpl
     * @param productNum
     * @Return: com.cloud.drore.eboos.common.entity.supplier.vo.ConsumeAndPoiForDealer
     * @Decription:
     * @CreateDate: Created in 2018/3/22 11:50
     * @Author: <a href="747639122@qq.com">冯腾</a>
     * @Modify:
     */
    @Override
    public ConsumeAndPoiForDealer findPoiAndConsumeForDealer(String productNum)throws BusinessException {
        ConsumeAndPoiForDealer poiAndConsumeForDealer;
        try {
            poiAndConsumeForDealer = productRelationMapper.findPoiAndConsumeForDealer(productNum);
        }catch (Exception e){
            e.printStackTrace();
            throw new BusinessException(8500,"查询数据库结果有误！");
        }
        if (poiAndConsumeForDealer == null){
            throw new BusinessException(8500,"查询数据库结果有误！");
        }
        return poiAndConsumeForDealer;
    }
}